package com.ruoyi.web.controller.system;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.quartz.domain.HatchetAcqJifen;
import com.ruoyi.quartz.service.IHatchetAcqJifenService;
import com.ruoyi.system.domain.HatchetAgent;
import com.ruoyi.system.domain.HatchetMerchant;
import com.ruoyi.system.domain.HatchetMoneyChange;
import com.ruoyi.system.domain.HatchetOrderPayment;
import com.ruoyi.system.service.IHatchetAgentService;
import com.ruoyi.system.service.IHatchetMerchantService;
import com.ruoyi.system.service.IHatchetMoneyChangeService;
import com.ruoyi.system.service.IHatchetOrderPaymentService;
import com.ruoyi.system.service.ISysDictDataService;

/**
 * 提现记录 信息操作处理
 * 
 * @author xj
 * @date 2019-02-18
 */
@Controller
@RequestMapping("/system/hatchetFenRun")
public class HatchetFenRunController extends BaseController {
	private String prefix = "system/hatchetMoneyChange";

	@Autowired
	private IHatchetMoneyChangeService hatchetMoneyChangeService;
	@Autowired
	private IHatchetAgentService agentService;
	@Autowired
	private IHatchetMerchantService merchantService;
	@Autowired
	private IHatchetOrderPaymentService hatchetOrderPaymentService;
	@Autowired
	private ISysDictDataService sysDictDataService ;
	@Autowired
	private IHatchetAcqJifenService acqJifenService;

	@RequiresPermissions("system:hatchetMoneyChange:view")
	@GetMapping()
	public String fenRunList() {
		return prefix + "/fenRunList";
	}

	/**
	 * 查询分润记录列表
	 */
	@RequiresPermissions("system:hatchetMoneyChange:list")
	@PostMapping("/fenRunList")
	@ResponseBody
	public TableDataInfo fenRunList(HatchetMoneyChange hatchetMoneyChange) {
		hatchetMoneyChange.setSTATUS("10B");
		BigDecimal sumMoney =  hatchetMoneyChangeService.selectSumMoney(hatchetMoneyChange);
		int index=0;
		startPage();
		List<HatchetMoneyChange> list = hatchetMoneyChangeService.selectFenRenChangeList(hatchetMoneyChange);
		int stringLength;
		for (HatchetMoneyChange hatchetMoneyChange2 : list) {
			if (index == 0) {
				hatchetMoneyChange2.setSumMoney(sumMoney);
				index ++;
			}
			if (hatchetMoneyChange2.getUserId() != null) {
				stringLength = hatchetMoneyChange2.getUserId().length();
				if (stringLength == 7) {
					HatchetAgent agent = agentService.selectHatchetAgentByNo(hatchetMoneyChange2.getUserId());
					if (agent != null) {
						hatchetMoneyChange2.setMerName(agent.getAgentName());
					}
				}
				if (stringLength == 15) {
					HatchetMerchant hatchetMerchant = merchantService.selectHatchetMerchantByNo(hatchetMoneyChange2.getUserId());
					if (hatchetMerchant != null) {
						hatchetMoneyChange2.setMerName(hatchetMerchant.getMerchantCnName());
					}
				}
			}
			//查询订单信息
			if (hatchetMoneyChange2.getPaymentOrderId() != null) {
				HatchetMerchant merchant = null;
				if ("10K".equals(hatchetMoneyChange2.getMoneyType())) {
					HatchetAcqJifen acqJifen = acqJifenService.selectJifenByOrderId(hatchetMoneyChange2.getPaymentOrderId());
					if (StringUtils.isNotNull(acqJifen)) {
						hatchetMoneyChange2.setCardNo(acqJifen.getOrderId());
						hatchetMoneyChange2.setCreateTime1(acqJifen.getSettleDate());
						hatchetMoneyChange2.setTradeType("积分兑换");
						merchant = merchantService.selectHatchetMerchantById(acqJifen.getUserId());
					}
				
				} else {
					HatchetOrderPayment orderPayment = hatchetOrderPaymentService.selectHatchetOrderPaymentByOrder(hatchetMoneyChange2.getPaymentOrderId());
					hatchetMoneyChange2.setCardNo(orderPayment.getCardNo());
					hatchetMoneyChange2.setCreateTime1(orderPayment.getCreateTime());
					hatchetMoneyChange2.setComplateTime(orderPayment.getCompleteTime());
					if (StringUtils.isNotNull(orderPayment.getRATE())) {
						hatchetMoneyChange2.setRate(orderPayment.getRATE().toString());
					}
					hatchetMoneyChange2.setTradeType(orderPayment.getTradeType());
					//获取交易商户名称
					merchant = merchantService.selectHatchetMerchantById(orderPayment.getMerchantId());
				}
				if (merchant != null) {
					hatchetMoneyChange2.setTradeMerName(merchant.getMerchantCnName());
					hatchetMoneyChange2.setMerchantId(merchant.getMerchantNo());
				}
			}
		}
		return getDataTable(list);
	}

	/**
	 * 导出分润记录列表
	 */
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(HatchetMoneyChange hatchetMoneyChange) {
		List<HatchetMoneyChange> list = hatchetMoneyChangeService.selectFenRenChangeList(hatchetMoneyChange);
		ExcelUtil<HatchetMoneyChange> util = new ExcelUtil<HatchetMoneyChange>(HatchetMoneyChange.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		int stringLength;
		for (HatchetMoneyChange hatchetMoneyChange2 : list) {
			hatchetMoneyChange2.setTime(sdf.format(hatchetMoneyChange2.getCreateTime()));
			hatchetMoneyChange2.setMoneyType(sysDictDataService.selectDictLabel("money_type", hatchetMoneyChange2.getMoneyType()));
			if (hatchetMoneyChange2.getUserId() != null) {
				stringLength = hatchetMoneyChange2.getUserId().length();
				if (stringLength == 7) {
					HatchetAgent agent = agentService.selectHatchetAgentByNo(hatchetMoneyChange2.getUserId());
					if (agent != null) {
						hatchetMoneyChange2.setMerName(agent.getAgentName());
					}
				}
				if (stringLength == 15) {
					HatchetMerchant hatchetMerchant = merchantService.selectHatchetMerchantByNo(hatchetMoneyChange2.getUserId());
					if (hatchetMerchant != null) {
						hatchetMoneyChange2.setMerName(hatchetMerchant.getMerchantCnName());
					}
				}
			}
			//查询订单信息
			if (hatchetMoneyChange2.getPaymentOrderId() != null) {
				if ("10K".equals(hatchetMoneyChange2.getMoneyType())) {
					//查询积分表
					HatchetAcqJifen acqJifen = acqJifenService.selectJifenByOrderId(hatchetMoneyChange2.getPaymentOrderId());
					if (StringUtils.isNotNull(acqJifen)) {
						hatchetMoneyChange2.setCardNo(acqJifen.getOrderId());
						hatchetMoneyChange2.setCreateTime1(acqJifen.getCreateTime());
						hatchetMoneyChange2.setTradeType("积分兑换");
						//获取交易商户名称
						HatchetMerchant merchant = merchantService.selectHatchetMerchantById(acqJifen.getUserId());
						if (merchant != null) {
							hatchetMoneyChange2.setTradeMerName(merchant.getMerchantCnName());
							hatchetMoneyChange2.setMerchantId(merchant.getMerchantNo());
						}
					}
				} else {
					HatchetOrderPayment orderPayment = hatchetOrderPaymentService.selectHatchetOrderPaymentByOrder(hatchetMoneyChange2.getPaymentOrderId());
					if (StringUtils.isNotNull(orderPayment)) {
						hatchetMoneyChange2.setCardNo(orderPayment.getCardNo());
						hatchetMoneyChange2.setCreateTime1(orderPayment.getCreateTime());
						hatchetMoneyChange2.setComplateTime(orderPayment.getCompleteTime());
						hatchetMoneyChange2.setRate(orderPayment.getRATE().toString());
						hatchetMoneyChange2.setTradeType(orderPayment.getTradeType());
						//获取交易商户名称
						HatchetMerchant merchant = merchantService.selectHatchetMerchantById(orderPayment.getMerchantId());
						if (merchant != null) {
							hatchetMoneyChange2.setTradeMerName(merchant.getMerchantCnName());
							hatchetMoneyChange2.setMerchantId(merchant.getMerchantNo());
						}
					}
				}
			}
		}
		return util.exportExcel(list, "分润记录表");
	}

}
